#include "stm32f10x.h"                  // Device header
#include "Delay.h"
uint16_t CountSensor_Count;

void CountSensor_Init(void){
	//开启时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//GPIO
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//AFIO
	// EXTI 和NVIC的时钟都是默认开启的   NVIC是内核外设，RCC只能管控外设时钟
	
	//配置GPIO
	GPIO_InitTypeDef GPIO_Init_Structure;
	GPIO_Init_Structure.GPIO_Mode = GPIO_Mode_IPD;//对于外部中断来说,浮空 上拉 或下拉
	GPIO_Init_Structure.GPIO_Pin = GPIO_Pin_10;
	GPIO_Init_Structure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_Init_Structure);
	
	//AFIO的库函数和GPIO在一个文件里  配置AFIO
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource10);
	
	//配置EXTI
	EXTI_InitTypeDef EXTI_Init_Structure;
	EXTI_Init_Structure.EXTI_Line = EXTI_Line10;
	EXTI_Init_Structure.EXTI_LineCmd = ENABLE;
	EXTI_Init_Structure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_Init_Structure.EXTI_Trigger = EXTI_Trigger_Falling;
	EXTI_Init(&EXTI_Init_Structure);
	
	//配置NVIC
	//配置中断之前，先指定NVIC的分组,然后再NVIC_Init
	//分组的方式 整个芯片只能用一种
	//多模块的分组需要相同
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 	//优先级的分组需要看实际需求
	
	NVIC_InitTypeDef NVIC_Init_Structure;
	NVIC_Init_Structure.NVIC_IRQChannel = EXTI15_10_IRQn;
	NVIC_Init_Structure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init_Structure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_Init_Structure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_Init_Structure);
	
}

uint16_t CountSensor_Get(void){
	return CountSensor_Count;
}
void EXTI15_10_IRQHandler(void){
	//判断中断标志位
	if(EXTI_GetITStatus(EXTI_Line10) == SET){
		
		CountSensor_Count += 1;
		// 每次调用后，应该清除中断标志位
		EXTI_ClearITPendingBit(EXTI_Line10);
		Delay_ms(30);
	}
	
}


